//
//  HBHotCoinChartXAxisView.m
//  FindClass
//
//  Created by tianlong on 2024/12/4.
//

#import "HBHotCoinChartXAxisView.h"
#import "HBHotCoinChartUtils.h"
#import "UIFont+helper.h"
#import "UIView+Extension.h"

@interface HBHotCoinChartXAxisView ()

@property (nonatomic, strong) NSArray *list;

@end

@implementation HBHotCoinChartXAxisView

- (instancetype)initWithChartModel:(HBHotCointChartModel *)chartModel {
    self = [super initWithChartModel:chartModel];
    if (self) {
        self.list = [HBHotCoinChartUtils findElementsFromArray:chartModel.xList];
#if DEBUG
        self.chartModel.xTextWidth = @29;
        self.chartModel.xTextHeight = @14;
#else
#endif
        self.height = self.chartModel.xTextHeight.floatValue;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    for (NSString *str in self.list) {
        UILabel *label = [[UILabel alloc] init];
        label.text = str;
        label.font = [UIFont font_DinRegularWithSize:10];
#if DEBUG
        label.font = [UIFont systemFontOfSize:10];
#else
#endif
        label.textColor = UIColor.blackColor;// eBaseColorFourLevelText
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat contentW = self.chartModel.xTextWidth.floatValue;
    CGFloat margin = (self.width - contentW * HotCoinChartXAxisSections) / (HotCoinChartXAxisSections - 1);
    for (NSInteger i = 0; i < self.subviews.count; i++) {
        UILabel *label = self.subviews[i];
        if ([label isKindOfClass:[UILabel class]]) {
            NSInteger idx = [self.subviews indexOfObject:label];
            label.frame = CGRectMake(idx * (contentW + margin), 0, contentW, self.chartModel.xTextHeight.floatValue);
        }
    }
}

@end
